{% extends ../base.html %}

{% block head %}
<style>

.sync-file {
    color: #000;
}

.sync-dir {
    color: blue;
}

.sync-diff {
    color: red;
}

.red {
    color: red;
}

.red a {
    color: red;
}

</style>
{% end %}

{% block body %}
    {% init days = 7 %}
    <div>
        <form>
            <div>链接 <textarea name="url" cols=100>{{?url}}</textarea></div>
            <div>目录 <textarea name="path" cols=100>{{?path}}</textarea></div>
            <div>同步天数 <input type="text" name="days" value="{{?days}}"/></div>
            <div> <button> 查询 </button> </div>
        </form>
    </div>

    {% if "diff_list" in globals() %}
    <div>
        文件差异
    </div>
    <table class="table" id="sync-list-table">
    <tr>
        <th>path</th>
        <th>size</th>
        <th>md5</th>
        <th>localsize</th>
        <th>localmd5</th>
        <th>operation</th>
    </tr>
    {% for idx, diff in enumerate(diff_list) %}
        <tr id="f_{{idx}}">
            <td> <a target="_blank" href="{{diff.url}}" class="{{diff['class']}}">{{diff.path}}</a> </td>
            <td>{{diff.size}} </td>
            <td>{{diff.md5}}</td>
            <td>{{diff.localsize}}</td>
            <td>{{diff.localmd5}}</td>
            <td> 
            {% if diff['type'] == 'file'%}
                <a class="{{diff['class']}}" data-url="{{diff.url}}" href='javascript:sync_file("/sync_file?path={{diff.path}}&url={{url}}", "f_{{idx}}")' target="_blank">同步到本地</a>
            {% end %}
            </td>
        </tr>
    {% end %}
    </table>
    {% end %}

    {% if "db_diff_list" in globals() %}
        <div>数据库差异</div>
        <table class="table">
            <tr>
                <td>名称</td>
                <td>修改时间</td>
                <td>本地修改时间</td>
                <td>操作</td>
            </tr>
        <!-- table -->
        {% for idx, file in enumerate(db_diff_list) %}
            <tr id="db_{{idx}}">
                <td>{{file.name}}</td>
                <td>{{file.smtime}}</td>
                <td>{{file.localsmtime}}</td>
                <td {% if file.localmtime < file.mtime %} class = "red" {% end %} >
                    <a href="javascript:sync_db('/sync_db?url={{url}}&name={{file.name}}', 'db_{{idx}}')">同步到本地</a></td>
            </tr>
        {% end %}
        </table>
    {% end %}
<script>
$(document).ready(function () {
    function sync_file(url, id) {
        $.get(url, function (data) {
            if (data == "success") {
                $("#" + id).remove();
            } else {
                alert("同步失败");
            }
        });
    }

    window.sync_file = sync_file;

    function sync_db(url, id) {
        console.log(url);
        url = url.replace(/\+/gi, "%2B");
        $.get(url, function (data) {
            if (data == "success") {
                $("#" + id).remove();
            } else {
                alert("同步失败, " + data);
            }
        });
    }
    window.sync_db = sync_db;
})
</script>
{% end %}